package com.capgemini.scaffold.service.impl;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.dto.InternDTO;
import com.capgemini.scaffold.dto.TutorDTO;
import com.capgemini.scaffold.entity.Intern;
import com.capgemini.scaffold.entity.Tutor;
import com.capgemini.scaffold.enums.GenderEnum;
import com.capgemini.scaffold.enums.JobsEnum;
import com.capgemini.scaffold.enums.StateEnum;
import com.capgemini.scaffold.mapper.TutorMapper;
import com.capgemini.scaffold.service.TutorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TutorServiceImpl extends ServiceImpl<TutorMapper, Tutor> implements TutorService {

    @Resource
    private TutorMapper tutorMapper;


    /**
     * 查询导师的所有数据
     * @return 返回导师数据集合
     */
    @Override
    public ResponseBean<List<Intern>> queryTutor() {
        List<Tutor> tutorList = tutorMapper.queryTutor();
        //字典查询
        for (Tutor record : tutorList) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }
        return ResponseBean.success(tutorList);
    }

    /**
     * 插入导师的数据
     * @param tutor 插入的数据
     * @return 收影响行数
     */
    @Override
    public Integer addNewTutor(Tutor tutor) {

        //字典替换数字插入
        String job = JobsEnum.getCodeByName(tutor.getJobName());
        tutor.setJob(job);

        String sex = GenderEnum.getCodeByName(tutor.getSexName());
        tutor.setSex(sex);


        //判断受影响行数不为一时候，抛出异常
        Integer row = tutorMapper.insert(tutor);
        if (row != 1){
            throw new BusinessException(ExceptionEnum. INSERTING_DATA_EXCEPTION);
        }
        return row;
    }

    /**
     * 修改数据
     * @param tutor 通过id查询到的对象
     * @return 返回受影响行数
     */
    @Override
    public Integer updateTutor(Tutor tutor) {
        //字典替换数字插入
        String Job = JobsEnum.getCodeByName(tutor.getJobName());
        tutor.setJob(Job);

        String sex = GenderEnum.getCodeByName(tutor.getSexName());
        tutor.setSex(sex);


//        //判断修改的数据是否为空，为空抛出异常
//        if (tutor.getName() == null){
//            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_NULL);
//        }
//        if (tutor.getSex() == null){
//            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_NULL);
//        }
//        if (tutor.getJobGrade() == null){
//            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_NULL);
//        }
//        if (tutor.getJob() == null){
//            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_NULL);
//        }
//        if (tutor.getMail() == null){
//            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_NULL);
//        }
        Integer row = tutorMapper.updateTutor(tutor);
        if (row != 1){
            throw new BusinessException(ExceptionEnum.UPDATE_TUTORDATA_EXCEPTION);
        }
        return row;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Tutor selectById(int id) {
        Tutor tutor = tutorMapper.selectById(id);
        //字典查询
        tutor.setSexName(GenderEnum.getNameByCode(tutor.getSex()));
        tutor.setJobName(JobsEnum.getNameByCode(tutor.getJob()));
        return tutor;
    }

    /**
     * 点击导师姓名查询导师的学员
     * @param req 分页
     * @param id 导师id
     * @return 返回分页数据
     */
    @Override
    public IPage<Intern> queryInternPage(InternDTO req, Integer id) {
        //封装分页信息，第一个参数页码值、第二个参数每页显示数量
        Page<Intern> page = new Page<>(req.getPageNum(), req.getPageSize());
        //进行分页查询
        IPage<Intern> pageList = tutorMapper.linkTutorAndIntern(page, req, id);
        //字典循环返回数据
        for (Intern record : pageList.getRecords()) {
            record.setStateName(StateEnum.getNameByCode(record.getState()));
        }
        //获取分req
        return pageList;
    }

    @Override
    public IPage<Tutor> queryTutor(TutorDTO req) {
        //封装分页信息，第一个参数页码值、第二个参数每页显示数量
        Page<Tutor> page = new Page<>(req.getPageNum(), req.getPageSize());
        //进行分页查询
        IPage<Tutor> pageList = tutorMapper.queryTutor(page, req);
        for (Tutor record : pageList.getRecords()) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }
        return pageList;
    }

    //模糊查询
    @Override
    public List<Tutor> linkTutor(TutorDTO req) {
        List<Tutor> tutorList = tutorMapper.linkTutor(req);
        //字典查询
        for (Tutor record : tutorList) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }
        return tutorList;
    }

    /**
     * 修改实习生导师id为需要删除导师的Id（要求改为学员最少的导师）并删除导师
     * @param id 导师的id
     * @return 返回多少名实习生导师呗修改
     */
    @Override
    public Integer deleteTutorId(Integer id) {
        //删除传入id的导师
        tutorMapper.deleteById(id);
        Integer upDataIntern = tutorMapper.updateTutorId(id);
        return upDataIntern;
    }

    /**
     * 待教学员的模糊查询
     * @param req 待教学员name模糊查询
     * @param id 传入的导师的id
     * @return 返回查询到的数据
     */
    @Override
    public List<Intern> linkIntern(InternDTO req, Integer id) {
        List<Intern> internList = tutorMapper.linkIntern(req,id);
        //查找状态回写
        for (Intern record : internList) {
            record.setStateName(StateEnum.getNameByCode(record.getState()));
        }
        return internList;
    }


}


